﻿using Microsoft.EntityFrameworkCore;
using S.BreedingHistory.Domain.MyDbContexts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.BreedingHistory.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 异步添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 异步删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var List = db.Set<T>().Find(id);
             db.Remove(List);
            return await db.SaveChangesAsync()>0;
        }
        /// <summary>
        /// 异步获取
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();    
        }
        /// <summary>
        /// 异步修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T t)
        {
            db.Update(t);
            return await db.SaveChangesAsync()>0;
        }
    }
}
